package com.leyou.putGoods.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.putGoods.entity.PutGoods;
import com.leyou.putGoods.entity.PutGoodsVo;
import com.leyou.putGoods.mapper.PutGoodsMapper;
import com.leyou.putGoods.service.IPutGoodsService;
import com.leyou.shop.entity.UpdateLog;
import com.leyou.shop.mapper.UpdateLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cyb
 * @since 2022-07-25
 */
@Service
public class PutGoodsServiceImpl extends ServiceImpl<PutGoodsMapper, PutGoods> implements IPutGoodsService {

    @Autowired
    private PutGoodsMapper putGoodsMapper;

    @Autowired
    UpdateLogMapper logMapper;


//    @Override
//    public int savePutGood(PutGoods putGoods, List<PutGoods> putGood) {
//        return putGoodsMapper.savePutGood(putGoods,putGood);
//    }
//
//    @Override
//    public Integer savePutGoods(PutGoodsVo putGoods) {
//        putGoodsMapper.savePutGoods(putGoods);
//        return null;
//    }

    @Override
    public int updateStatu(UpdateLog updateLog) {
        logMapper.addLog(updateLog);
        int i = putGoodsMapper.updateStatu(updateLog.getId());
        return i;
    }

    @Override
    public int savePutGood(List<PutGoods> putGoods) {
        return 0;
    }

    @Override
    @Transactional
    public int savePutGoods(List<PutGoods> putGoods) {
        System.err.println(putGoods.toString());
        System.err.println(putGoods.size());
        if (putGoods.size() > 1){
            putGoods.get(0).setOrederNumber();
            putGoodsMapper.addPid(putGoods.get(0));
            Integer id = putGoods.get(0).getId();
            for (PutGoods putGoodss : putGoods) {
                putGoodss.setPid(id);
                putGoodss.setOrederNumber();
                putGoodsMapper.addList(putGoodss);
            }
        }else {
            putGoods.get(0).setOrederNumber();
            putGoods.get(0).setPid(0);
            putGoodsMapper.addList(putGoods.get(0));
        }
        return 0;
    }

    @Override
    public List<PutGoods> findTree(String endTime, String startTime, String statu) {
        QueryWrapper<PutGoods> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(statu)){
            queryWrapper.like("statu",statu);
        }
        if (StrUtil.isNotBlank(startTime)){
            queryWrapper.ge("create_time",startTime);
        }
        if (StrUtil.isNotBlank(endTime)){
            queryWrapper.le("create_time",endTime);
        }
//        queryWrapper.orderByDesc("id");
        List<PutGoods> list = list(queryWrapper);


        /*for (PutGoods putGoodsParent:list){
            for (PutGoods putGoods:list){
                if(putGoodsParent.getId() == putGoods.getPid()){
                    List<PutGoods> children = putGoodsParent.getChildren();
                    if(children==null){
                        children = new ArrayList<PutGoods>();
                        children.add(putGoods);
                    }else{
                        children.add(putGoods);
                    }
                    putGoodsParent.setChildren(children);
                }
            }
        }
        for (int i = list.size()-1;i >= 0 ;i--){
            if(list.get(i).getPid() != 0){
                list.remove(i);
            }
        }
        System.err.println(list);
        System.err.println(list.size());*/

        Stream<PutGoods> putGoodsStream = list.stream().filter(putGoods -> putGoods.getPid() == 0);
        //找出pid为null的一级菜单
        List<PutGoods> parentNodes = putGoodsStream.collect(Collectors.toList());
        //找出一级菜单的子菜单
        for (PutGoods putGoods : parentNodes) {
            putGoods.setChildren(list.stream().filter(m -> putGoods.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return parentNodes;
    }
}
